/**
 * 
 */
var Page = function() {
	return {
		init:function(options){
			var settings = $.extend({}, options);
			var totalPages = settings.totalPages || 1;
			var currentPage = settings.currentPage || 1;
		    $.jqPaginator('#pagination', {
		        totalPages: parseInt(totalPages),
		        visiblePages: 8,
		        currentPage: parseInt(currentPage),
		        first: '<li class="first"><a href="javascript:;">首页</a></li>',
		        prev: '<li class="prev"><a href="javascript:;"><i class="arrow arrow2"></i>上一页</a></li>',
		        next: '<li class="next"><a href="javascript:;">下一页<i class="arrow arrow3"></i></a></li>',
		        last: '<li class="last"><a href="javascript:;">末页</a></li>',
		        page: '<li class="page"><a href="javascript:;">{{page}}</a></li>',
		        onPageChange: function (currentPage,type) {
		        	if(type=="change"){
		        		Page.exeData(currentPage);
		        	}
		        }
		    });
		},
		exeData: function (currentPage) {
			if(parseInt($(".totalPage").val())<currentPage){
				Page.prompt("分页查询错误！");
				retrun;
			}
			var keywords = $('#keywords').val();
			if("请输入搜索应用"===keywords){
				keywords="";
			}
			var sortType = $('#sortType').val();
			var sortMode = $('#sortMode').val();
			var method = $(".method").val();
			var params  = {"currentPage":currentPage, "showSize":10, "sortMode":sortMode, "sortType":sortType, "keywords":keywords};
			if("accountList"==method){
				params = {"currentPage":currentPage, "showSize":10, "sortMode":sortMode, "sortType":sortType, "keywords":keywords,"appName":$(".appName").val()};
			}
			$.ajax({
				type : "post",
				url : 	"/economy/web/"+method,
				data : params,
				dataType : "json",
				async : true,
				success : function(data) {
					var contentText = '';
					if(data.status===false){
						Page.noData(contentText,keywords);
						Page.prompt(data.message);
					}
					//遍历组装table
					if("apppage"==method){
						contentText = Page.appEach(data);
						$(".app_total").html(data.returnObject.totalrecord);
						if(data.returnObject.data==null||data.returnObject.data==""){
							data.returnObject.data=0;
						}
						$(".app_normal").html(data.returnObject.data);
						$(".app_freeze").html(parseInt(data.returnObject.totalrecord)-parseInt(data.returnObject.data));
					}else if("accountList"==method){
						contentText = Page.accountEach(data);
					}
					//处理没有数据
					Page.noData(contentText,keywords);
					if(''=== contentText){
						return;
					}
					//填充数据
					Page.showData(contentText,data.returnObject.totalpage,data.returnObject.pagenum);
				},
				error : function() {
					Page.prompt("刷新失败！");
				}
			});
		},
		appEach: function (data) {
			var contentText = '';
			$.each(data.returnObject.list, function(index,item){ 
				if(index==0){
					contentText += "<tr style=\"line-height: 5px;background-color: #fafafa;\"><td colspan=\"7\" height=\"5px;\"></td></tr><tr style=\"line-height: 14px;background-color: #fafafa;\">";
				}else{
					contentText += "<tr ><td colspan=\"7\" height=\"14px;\"></td></tr><tr style=\"line-height: 14px;background-color: #fafafa;\">";
				}
				var num = parseInt(index)+1+(parseInt(data.returnObject.pagenum)-1)*10;
				contentText += "<td>" + num +"</td>" +
						"<td class=\"ellipsis\" >" + item.appName + "</td>" +
						"<td>" + item.account_count + "</td>" +
						"<td>" + item.auth_count + "</td>" + 
						"<td>"+item.createTime+"</td>";
				if (item.status == '0') {
					contentText += "<td><span style=\"color: #07b2dd;\">正常</span></td>";
				} else {
					contentText += "<td><span style=\"color: #df516b;\">冻结</span></td>";
				}
				var statuStr = "解冻应用";
				var state = true;
				if(item.status=="0"){
					statuStr="冻结应用";
					state = false;
				}
				contentText += "<td><a href='#' onclick=\"Page.updateState('"+state+"','"+item.appKey+"',this)\">"+statuStr+"</a> <a href='toPage?page=account_list&data="+item.appKey+","+item.appName+"' style='margin-left: 5%;'>查看帐号</a></td></tr>";
			});
			return contentText;
		},
		accountEach: function (data){
			var contentText = '';
			$.each(data.returnObject.list, function(index,item){
				if(index==0){
					contentText += "<tr style=\"line-height:5px;background-color: #fafafa;\"><td colspan=\"5\" height=\"5px;\"></td></tr><tr style=\"line-height: 14px;background-color: #fafafa;\">";
				}else{
					contentText += "<tr ><td colspan=\"5\" height=\"14px;\"></td></tr><tr style=\"line-height: 14px;background-color: #fafafa;\">";
				}
				var num = parseInt(index)+1+(parseInt(data.returnObject.pagenum)-1)*10;
				contentText += "<td>" + num +"</td>" +
						"<td class=\"ellipsis\">" + item.account + "</td>" +
						"<td>" + item.accountCount + "</td>" + 
						"<td>"+item.registerTime+"</td>" +
						"<td><a href='toPage?page=device_info&data="+$(".appKey").val()+","+item.account+","+$(".appName").val()+"'>查看设备</a></td></tr>";
			});
			return contentText;
		},
		noData: function(contentText,keywords){
			if(''===contentText && keywords===""){
				$(".list_div").css("display","none");
				$(".search_display").css("display","none");
				$(".not_data").css("display","block");
				$(".no_data_img").css("display","block");
				$(".no_search_img").css("display","none");
			}
			if(''===contentText && keywords!=""){
				$(".list_div").css("display","none");
				$(".not_data").css("display","block");
				$(".no_data_img").css("display","none");
				$(".no_search_img").css("display","block");
			}
		},
		showData: function(contentText,totalpage,pagenum){
			$(".list_div").css("display","block");
			$(".search_display").css("display","block");
			$(".not_data").css("display","none");
			$("tbody").html(contentText);
			if(totalpage=="0"){
				$(".totalPage").val(1);
			}else{
				$(".totalPage").val(totalpage);
			}
			$("#currentPage").val(pagenum);
			//分页
			Page.init({
				totalPages : totalpage,
				currentPage : pagenum
			});
		},
		updateState: function(state,appKey,obj){
			var params = {"appKey":appKey,"state":state};
			$.ajax({
				type : "post",
				url : 	"/economy/web/appstate",
				data : params,
				dataType : "json",
				async : true,
				success : function(data) {
					if(data.status===false){
						Page.prompt(data.message);
						return;
					}
					var spanObj = $(obj).parent().parent().find('span');
					if(state=="true"){
						$(obj).html("冻结应用");
						$(obj).removeAttr("onclick");
						$(obj).attr("onclick","Page.updateState('false','"+appKey+"',this)");
						$(spanObj).html("正常");
						$(spanObj).css("color","#07b2dd");
					}else{
						$(obj).html("解冻应用");
						$(obj).removeAttr("onclick");
						$(obj).attr("onclick","Page.updateState('true','"+appKey+"',this)");
						$(spanObj).html("冻结");
						$(spanObj).css("color","#df516b");
					}
					Page.prompt("修改状态成功！");
				},
				error : function() {
					Page.prompt("刷新失败！");
				}
			});
		},
		prompt: function (message){
            $(".prompt-message").html(message);
            $("#prompt").modal();
		}
	};
	
}();